<?php

namespace Admin\Controller;

use Think\Controller;

class SpecialcourseController extends Controller {

  public function delSpecialCourse($id)
  {
    $Hint = new \Operation\Controller\HintController (); // 实例化Hint
    $Hint->checkLogin ( 3 ); // 验证是否登陆
    $where['scid']=$id;
    $re=M('specialcourse')->where($where)->delete();
    if ($re) {
      $Hint->mysuccee ( "删除成功！","2" );
    }
    else{
        $Hint->myerror('删除失败' );
    }    
  }
  public function showList()
  {
    $number=1;
     $specialC=D('specialcourse');
     $list=$specialC->select();
     $special=D('special')->field('sid,sname,majorteacher')->select();
     // dump($special);die;
     $this->assign(array('list'=>$list,'special'=>$special));
     $this->assign('number',$number);
     $this->display();
  }

 public function edit($scid)
  {
    $Hint = new \Operation\Controller\HintController (); // 实例化Hint
    $Hint->checkLogin ( 3 );
    $specialC=D('specialcourse');
  if (IS_POST) {
          $data['scid']=$scid;
          $data['scname']=$_POST['scname'];
          $data['sid']=$_POST['sid'];
          $data['startdate']=$_POST['startdate'];
          $data['enddate']=$_POST['enddate'];
          $data['place']=$_POST['place'];
          $data['content']=$_POST['content'];
          $data['totalticket']=$_POST['totalticket'];
            if ($specialC->create()) {
              if($specialC->save($data)!==false){
                 $Hint->mysuccee ( "修改成功！","2" );
                }else{
                  $Hint->myerror("修改失败");
                }
            }
        return;
        }
    $specialres=$specialC->find($scid);
    $special=D('special')->field('sid,sname')->select();
    $this->assign(array('list'=>$specialres,'special'=>$special));
    $this->display();
  }
   public function del($scid)
   {
      $specialC=D('specialcourse');
     $res=$specialC->delete($scid);
    if ($res) {
      $this->success('删除成功',U('showlist'));
    }else{
      $this->error('删除失败');
    }
   }

  public function add()
   {
    $specialC=D('specialcourse');
    if (IS_POST) {
        $data['scname']=$_POST['scname'];
        $data['sid']=$_POST['sid'];
        $data['startdate']=$_POST['startdate'];
        $data['enddate']=$_POST['enddate'];
        $data['place']=$_POST['place'];
        $data['content']=$_POST['content'];
        $data['totalticket']=$_POST['totalticket'];
        if($specialC->add($data)){
            $this->success('添加成功！',U('showlist'));
          }else{
            $this->error('添加失败！');
          }
      return;
      }
     $list=D('special')->field('sid,sname')->select();
     $this->assign('list',$list);
     $this->display();
   }


 public function stutable($scid)
   {
    $where['scid']=$_GET['scid'];
    $model=M('specialcourse');
    $detail=$model->where($where)->select();
    $stu_course=D('stu_course');
    $stulist=$stu_course->field('a.ssid,a.state,a.score,b.realname,b.stunum')->alias('a')->join('users b ON a.uid=b.uid')->where(array('scid'=>$scid))->order('ssid desc')->select();
    $this->assign('stulist',$stulist);
    $this->assign('detail',$detail);
   $this->display();
   }

  public function stuedit(){
    $data=$_GET;
    $stu_activity=D('stu_course');
    $res=$stu_activity->save($data);
    if ($res) {
      echo "1";
    }else{
      echo "0";
    }
  }

  // 导入学生名单
public function instu(){
    $scid=I('scid');
    if (IS_POST) {
      header("Content-Type:text/html;charset=utf-8");
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('xls', 'xlsx');// 设置附件上传类
        $upload->savePath  =      'Public/UPLOADS/excelData/'; // 设置附件上传目录
        $upload->rootPath  =      './'; 
        // 上传文件
        $info   =   $upload->uploadOne($_FILES['excelData']);
        $filename =$info['savepath'].$info['savename'];
        $exts = $info['ext'];
        if(!$info) {// 上传错误提示错误信息
              $this->error($upload->getError());
          }else{// 上传成功
                // dump('1');die;
            $this->goods_import($filename, $exts);
          }
    
          return;
    }
      $this->assign('scid',$scid);
      $this->display();
}

//导入数据方法
    protected function goods_import($filename, $exts='xls')
    {
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
        import("Org.Util.PHPExcel");
        //创建PHPExcel对象，注意，不能少了\
        $PHPExcel=new \PHPExcel();
        //如果excel文件后缀名为.xls，导入这个类
        if($exts == 'xls'){
            import("Org.Util.PHPExcel.Reader.Excel5");
            $PHPReader=new \PHPExcel_Reader_Excel5();
        }else if($exts == 'xlsx'){
            import("Org.Util.PHPExcel.Reader.Excel2007");
            $PHPReader=new \PHPExcel_Reader_Excel2007();
        }


        //载入文件
        $PHPExcel=$PHPReader->load($filename);
        //获取表中的第一个工作表，如果要获取第二个，把0改为1，依次类推
        $currentSheet=$PHPExcel->getSheet(0);
        //获取总列数
        $allColumn=$currentSheet->getHighestColumn();
        //获取总行数
        $allRow=$currentSheet->getHighestRow();
        //循环获取表中的数据，$currentRow表示当前行，从哪行开始读取数据，索引值从0开始
        $arr=array();
        for($currentRow=2;$currentRow<$allRow;$currentRow++){
            //从哪列开始，A表示第一列
            $date=null;
            for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
                //数据坐标
                $address=$currentColumn.$currentRow;
                //读取到的数据，保存到数组$arr中
                $cell =$currentSheet->getCell($address)->getValue();
                //$cell = $data[$currentRow][$currentColumn];
                if($cell instanceof PHPExcel_RichText){
                    $cell  = $cell->__toString();
                }
              
               $date[]=$cell;
            }
                
                $arr[$currentRow]=$date;
        } 
// dump($arr);die;
        $this->save_import($arr);
    }

    public function save_import($arr){
        // dump($arr);die;
        $stu_activity=D('stu_course');
        foreach ($arr as $k => $v) {
      $data = array('score'=>$v[3],'state'=>$v[4]);
      $stu_activity->where(array('ssid'=>$v[0]))->setField($data);  
          $data=null;
        } 
        $this->success('导入成功',U('showlist'));

    }

//  导出学生名单方法
  public function exstu($scid,$scname){
    $filename=$scname;
    $stu_course=D('stu_course');
    $stulist=$stu_course->field('a.ssid,a.state,a.score,b.realname,b.stunum')->alias('a')->join('users b ON a.uid=b.uid')->where(array('scid'=>$scid))->order('ssid asc')->select();
    $data=array();
    foreach ($stulist as $k => $v) {
      $data[$k]['ssid']=$v['ssid'];
      $data[$k]['stunum']=$v['stunum'];
      $data[$k]['realname']=$v['realname'];
      $data[$k]['score']=$v['score'];
      $data[$k]['state']=$v['state'];
    }
     $headArr[]='ID';
     $headArr[]='学号';
     $headArr[]='姓名';
     $headArr[]='分数';
     $headArr[]='出勤状态';
    $this->getExcel($filename,$headArr,$data);
  }

    private  function getExcel($fileName,$headArr,$data){
        //导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");

        $fileName .= ".xls";

        //创建PHPExcel对象，注意，不能少了\
        $objPHPExcel = new \PHPExcel();
        $objProps = $objPHPExcel->getProperties();

        //设置表头
        $key = ord("A");
        //print_r($headArr);exit;
        foreach($headArr as $v){
            $colum = chr($key);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
            $objPHPExcel->setActiveSheetIndex(0) ->setCellValue($colum.'1', $v);
            $key += 1;
        }

        $column = 2;
        $objActSheet = $objPHPExcel->getActiveSheet();

        //print_r($data);exit;
        foreach($data as $key => $rows){ //行写入
            $span = ord("A");
            foreach($rows as $keyName=>$value){// 列写入
                $j = chr($span);
                $objActSheet->setCellValue($j.$column, $value);
                $span++;
            }
            $column++;
        }

        $fileName = iconv("utf-8", "gb2312", $fileName);

        //重命名表
        //$objPHPExcel->getActiveSheet()->setTitle('test');
        //设置活动单指数到第一个表,所以Excel打开这是第一个表
        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();//清除缓冲区,避免乱码
        header('Content-Type: application/vnd.ms-excel');
        header("Content-Disposition: attachment;filename=\"$fileName\"");
        header('Cache-Control: max-age=0');

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output'); //文件通过浏览器下载
        exit;
    }












}